const Controller = require('egg').Controller;
const svgCaptcha = require('svg-captcha')
const path = require("path")
const fs = require("fs")
class UserController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  async verify() {
    //生成验证码
    var captcha = svgCaptcha.create({
      size: 4,
      fontSize: 50,
      width: 100,
      height: 30,
      background: '#CDECF9'
    })
    //把验证码中文文字缓存起来
    this.ctx.session.verify = captcha.text
    //把验证码svg字符串发给请求者
    this.ctx.body = captcha.data
  }
  async login() {
    let res = this.ctx.request.body;
    // 判断数据库是否拥有该用户
    const sql = `SELECT * FROM graduation.user WHERE email="${res.email}"`;
    let res_sql1 = await this.app.mysql.query(sql);
    // console.log(res_sql1)
    if (!res_sql1[0]) {
      // 如果不存在 则返回code 码
      this.ctx.body = { info: "未注册，请注册", code: 4003 }
    } else {
      // 判断邮箱和密码是否匹配
      const sql2 = `SELECT * FROM graduation.user WHERE password="${res.password}"`;
      let res_sql2 = await this.app.mysql.query(sql2);
      if (!res_sql2[0]) {
        this.ctx.body = { info: "密码错误", code: 4004 }
      } else {
        console.log('我的res', this.ctx.session.verify)
        if (res.verify.toUpperCase() != this.ctx.session.verify.toUpperCase()) {
          this.ctx.body = { info: "验证码错误", code: 4001 }
        } else {
          // 缓存用户id
          this.ctx.session.id = res_sql2[0].id;
          this.ctx.body = { info: "登录成功", code: 2002 }
        }

      }
    }
  }
  async getUserInfo() {
    const { ctx } = this;
    let res = ctx.request.body;
    let sql = `SELECT * FROM graduation.user WHERE email="${res.email}"`;
    let data = await this.app.mysql.query(sql);
    this.ctx.body = data;
  }
  async login2() {
    const { ctx } = this;
    //前端传数据给后端
    var reqdata = ctx.request.query
    if (reqdata.verify.toUpperCase() != ctx.session.verifytext.toUpperCase()) {
      ctx.body = { info: '验证码错误', code: 4001 }
    } else {
      ctx.body = { info: '登录成功', code: 200 }
    }
  }
  //判断用户是否注册
  async rootlogin() {
    // const {ctx}=this
    // console.log("11111111111111111111111111111111");
    // let queryobj=ctx.request.body
    // console.log(queryobj,"6666666666666666");
    // const{id,pwd}=ctx.request.body
    // console.log(id,pwd,"55555555555555555555555555",ctx.service);
    // let data=await ctx.service.root.rootlogin(queryobj.id,queryobj.pwd)
    // if(data[0]){
    //   ctx.body={code:2001};
    // }else{
    //   ctx.body={code:5001}
    // }
    const { ctx, app } = this  //解构
    const { id, pwd, email } = ctx.request.body    //接收前端传过来的数据
    // console.log(id,pwd,email,'55555555555555555');
    let sqldata = `select * from root where email="${email}"`   //查找数据库
    let res = await app.mysql.query(sqldata)
    // console.log(res);
    if (res[0]) {
      ctx.body = { info: "邮箱已经存在", code: 4001 }
    } else {
      const sql = `insert into root (id,pwd,email) values("${id}","${pwd}","${email}")`
      let res1 = await this.app.mysql.query(sql)
      ctx.body = { info: "注册成功", code: 2001 }
    }
  }

  //注册
  async register() {
    // 1.验证验证码是否正确
    const { ctx } = this;
    //前端传数据给后端
    var reqdata = ctx.request.body
    if (reqdata.verify.toUpperCase() != ctx.session.verifytext.toUpperCase()) {
      ctx.body = { info: '验证码错误', code: 4001 }
    }
    else {
      //2.验证数据库中某些字段是否已经存在
      var sqlquery = `select * from user where email="${reqdata.email}"`
      var res1 = await this.app.mysql.query(sqlquery)
      console.log(res1);
      if (res1[0]) {
        ctx.body = { info: '邮箱已经注册', code: 4002 }
      } else {
        //   3.没有注册过的，添加到数据库中
        //   3.1把用户上传的头像生成网址
        var oldpath = ctx.request.files[0].filepath
        var newpath = __dirname + "/../public/upload/" + path.basename(oldpath)
        // fs.renameSync(oldpath, newpath, () => { })
        reqdata.headpic = "/public/upload/" + path.basename(oldpath)
        //   3.2把所有数据存到数据库中
        var sqladd = `insert into user (usename,password,headpic,email) values(
          "${reqdata.usename}","${reqdata.pwd}","${reqdata.headpic}", "${reqdata.email}")`
        await this.app.mysql.query(sqladd)
        ctx.body = { info: "注册成功", code: 2001 }
      }
    }
  }
  async hotpic() {
    const { ctx } = this;
    //前端传过来的数据
    let pagenumber = ctx.request.query.day
    //后端请求数据库的数据
    // let data = await ctx.service.home.home.index(pagenumber)
    const sql = `select * from hot where page="${pagenumber}"`
    const res = await this.app.mysql.query(sql)
    let data = res
    ctx.body = data
  }
  async photographer() {
    const { ctx } = this
    // let recommend=ctx.params.type
    let recommend = ctx.request.query.type
    const sql = `select * from photographer where type="recommend"`
    const res = await this.app.mysql.query(sql)
    let data = res
    ctx.body = data

  }
  async pushOpus() {
    const { ctx } = this;
    var res = ctx.request.body;
    let sql = `INSERT INTO graduation.selected (img,text,phone,time,userimg,username,userid) VALUES ('${res.img}','${res.text}','${res.phone}','${res.time}','${res.userimg}','${res.username}','${res.userid}')`
    const data = await this.app.mysql.query(sql)
    ctx.body = data;
  }
  async opusData() {
    const { ctx } = this;
    var res = ctx.request.body;
    let sql = `SELECT * FROM graduation.selected WHERE userid=${res.userid};`
    const data = await this.app.mysql.query(sql)
    ctx.body = data;
  }
  async searchData() {
    const { ctx } = this;
    var res = ctx.request.body;
    let sql = ``;
    if (res.search) {
      console.log('res.search');
      sql = `SELECT * FROM graduation.selected WHERE module='${res.search}' OR text LIKE '%${res.search}%';`
    } else {
      sql = `SELECT * FROM graduation.selected`
    }
    const data = await this.app.mysql.query(sql)
    ctx.body = data;
  }
  async labelData() {
    const { ctx } = this;
    var res = ctx.request.body;
    let sql = ``;
    if (res.style != '设备') {
      sql = `SELECT * FROM graduation.label WHERE style='${res.style}' and label_type='${res.label_type} ' LIMIT 3;`;
    } else {
      sql = `SELECT * FROM graduation.label WHERE phone='${res.label_type} ' LIMIT 3;`;
    }
    let data = await this.app.mysql.query(sql)
    ctx.body = data;
  }
  async oreDetailData() {
    const { ctx } = this;
    var res = ctx.request.body;
    let sql = ``;
    console.log(res.style);
    if (res.style != '设备') {
      // sql = `SELECT * FROM graduation.label WHERE style='${res.key}';`;
      sql = `SELECT label.id,label.label_type,label.img,label.name,label.comment,label.agree,label.time,label.phone,label.style,label.userid,user.headpic,user.username FROM graduation.label,graduation.user WHERE label_type='${res.key}' and label.userid=user.id;`;

    } else {
      // sql = `SELECT * FROM graduation.label WHERE phone='${res.key}';`;
      sql = `SELECT label.id,label.label_type,label.img,label.name,label.comment,label.agree,label.time,label.phone,label.style,label.userid,user.headpic,user.username FROM graduation.label,graduation.user WHERE phone='${res.key}' and label.userid=user.id;`;

    }
    let data = await this.app.mysql.query(sql)
    ctx.body = data;
  }

  async commentData() {
    const { ctx } = this;
    var res = ctx.request.body;
    let sql = `INSERT INTO graduation.comment_table (user_id,commented_id,comment_content,comment_time) VALUES (${res.user_id},${res.commented_id},'${res.comment_content}',${res.comment_time});`;
    let data = await this.app.mysql.query(sql)
    ctx.body = data;
  }
  async commentList() {
    const { ctx } = this;
    var res = ctx.request.body;
    let sql = `SELECT comment_table.comment_id,headpic,username,comment_content,comment_time,user_id,commented_id FROM graduation.comment_table,graduation.user WHERE user_id=user.id AND commented_id=${res.commented_id};`;
    let data = await this.app.mysql.query(sql)
    ctx.body = data;
  }
  async delComment() {
    const { ctx } = this;
    var res = ctx.request.body;
    let sql = `DELETE FROM graduation.comment_table WHERE comment_id=${res.id}`;
    let data = await this.app.mysql.query(sql)
    ctx.body = data;
  }
}

module.exports = UserController;
